using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Adge.Common.Config;
using Adge.Common;

namespace Adge.Web.UI.DesktopModules.Html
{
    public partial class coversShow : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Get a ClientScriptManager reference from the Page class.
            ClientScriptManager cs = Page.ClientScript;
            cs.RegisterClientScriptInclude("dw_rotator", this.ResolveClientUrl("~/js/dw_rotator.js"));
            cs.RegisterStartupScript(typeof(coversShow), "initRotator", buildRotator(), true);
            if (!Page.IsPostBack)
            {
                if (Covers.Count > 0)
                {
                    imgCover.ImageUrl = this.ResolveUrl(AdgeConfig.FileSoragePath + Covers[0].OriRelationPath);
                }
            }

        }

        #region Rotator Images

        IList<Cover> Covers
        {
            get
            {
                CoverConfig cover = SingletonProvider<CoverConfig>.Instance;
                return cover.GetImages();
            }
        }

        string buildRotator()
        {
            CoverConfig cover = SingletonProvider<CoverConfig>.Instance;
            StringBuilder initRotator = new StringBuilder("function initRotator() {");
            initRotator.AppendFormat("var rotator1 = new dw_Rotator('{0}', 3500, '{1}', 'cover');", imgCover.ClientID, this.ResolveClientUrl(cover.ImageDirectory));
            initRotator.Append("rotator1.addImages(");
            int imgcount = Covers.Count;
            for (int imgindex = 0; imgindex < imgcount; imgindex++)
            {
                initRotator.Append("\"");
                initRotator.Append(cover.GetImages()[imgindex].Filename);
                initRotator.Append("\"");
                if (imgindex < cover.GetImages().Count - 1)
                {
                    initRotator.Append(",");
                }
            }
            initRotator.Append(");rotator1.addActions(");
            for (int imgindex = 0; imgindex < imgcount; imgindex++)
            {
                initRotator.Append("\"");
                initRotator.AppendFormat("{0}?relPath={1}", this.ResolveClientUrl("~/pictureViewer.aspx"), this.ResolveUrl(AdgeConfig.FileSoragePath + Covers[imgindex].OriRelationPath));                
                initRotator.Append("\"");
                if (imgindex < cover.GetImages().Count - 1)
                {
                    initRotator.Append(",");
                }
            }
            //"rotator1.addActions("happy.html", "blue.html", "red.html", function() { demoFn(0); } );"
            initRotator.Append(");dw_Rotator.start();\n\t} ");
            if (imgcount > 0)
            {
                initRotator.Append("\n initRotator();");
            }
            return initRotator.ToString();
        }

        
        #endregion
    }
}